package com.ovopark.organize.server.controller;

import com.ovopark.organize.common.base.BaseResult;
import com.ovopark.organize.server.common.support.LocalStorageUtils;
import com.ovopark.organize.server.model.entity.EnterpriseGroup;
import com.ovopark.organize.server.model.entity.FranchiseesApprovalAddress;
import com.ovopark.organize.server.model.vo.FranchiseeAccurateAddressSaveVo;
import com.ovopark.organize.server.service.FranchiseesAccurateAddressService;
import com.ovopark.organize.server.service.FranchiseesApprovalAddressService;
import com.ovopark.organize.server.util.LogUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Locale;

/**
 * 加盟商确址
 */
@Slf4j
@RestController
@RequestMapping("/franchiseesAccurateAddress")
public class FranchiseesAccurateAddressController {

    @Resource
    private DepartmentsService departmentsService;

    @Resource
    private UserService userService;

    @Resource
    private FranchiseesAccurateAddressService franchiseesAccurateAddressService;
    @Resource
    private FranchiseesApprovalAddressService franchiseesApprovalAddressService;
    @Resource
    private EnterpriseGroupService enterpriseGroupService;
    /**
     * 确址
     */

    @PostMapping("/saveAccurateAddress")
    public BaseResult saveAccurateAddress(@RequestBody FranchiseeAccurateAddressSaveVo param, HttpServletRequest request) {
        Integer userId = LocalStorageUtils.getStorageInfo().getUserId();
        Integer curGroupId = LocalStorageUtils.getStorageInfo().getGroupId();
        if (userId == null) {
            return BaseResult.invalidToken();
        }
        if (curGroupId != null) {
            param.setGroupId(curGroupId);
        }
        if (param == null || param.getFranchiseesApprovalAddressId() == null || StringUtils.isEmpty(param.getUserName()) || StringUtils.isEmpty(param.getShowName()) || StringUtils.isEmpty(param.getDepartmentName())) {
            return BaseResult.paramError();
        }


        // 先校验 门店名称 门店编码 用户登录名称 是否有重复

        //Integer groupId,String name,String shopId,Integer depId
        Boolean checkDep = departmentsService.checkDepNameAndShopId(param.getGroupId(),param.getDepartmentName(),param.getShopId(),param.getDepartmentId());
        if(checkDep){
            return BaseResult.error("401","门店名称或门店编码已存在");
        }

        // 加 企微后缀 校验
        EnterpriseGroup enterpriseGroup = enterpriseGroupService.getEnterpriseById(param.getGroupId());
        if(enterpriseGroup == null){
            return BaseResult.paramError();
        }
        String suffix = enterpriseGroup.getSuffix();
        if(StringUtils.isNotEmpty(suffix) && param.getUserId() ==null){
            param.setUserName(param.getUserName().trim()+ "@" + suffix);
        }
        Boolean checkUser = userService.isNameDuplicate(param.getUserName(),param.getUserId());
        if(checkUser){
            return BaseResult.error("402","用户名已存在");
        }
        FranchiseesApprovalAddress approvalAddress = franchiseesApprovalAddressService.getById(param.getFranchiseesApprovalAddressId());
        if(approvalAddress ==null){
            return BaseResult.error("403","选址点位不存在");
        }
        Locale locale = LocalStorageUtils.getStorageInfo().getLocale();
        String remoteIp = LogUtils.getRemoteIp(request);
        String platform = LogUtils.getPlatform(request);
        BaseResult result =  franchiseesAccurateAddressService.saveAccurateAddress(param,userId,remoteIp,platform,  locale);

        return result;
    }











}
